﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using ImageResizer.FileExplorer.Implementations;
using ImageResizer.FileExplorer.Interface;
using ImageResizer.Outputs;
using ImageResizer.Outputs.Destination;
using ImageResizer.Profiles;
using ImageResizer.Properties;

namespace ImageResizer
{
    public partial class OptionsDeployFileExplorer : Form, IOptionsProvider
    {

        #region Private vars
        /// <summary>
        /// Gestionnaire de profiles
        /// </summary>
        private ProfilesManager _profilesManager;

        /// <summary>
        /// Contient la liste des sorties
        /// </summary>
        List<Output> _selectedOutputs;
        #endregion

        #region Interface (public) properties
        /// <summary>
        /// Obtient la liste des sorties voulues
        /// </summary>
        public List<Output> Outputs
        {
            get { return _selectedOutputs; }
        }

        /// <summary>
        /// Obtient le résultat de la boite de dialogue d'options
        /// </summary>
        public OptionsResults Result { get; private set; }

        /// <summary>
        /// Obtient l'objet permettant de manipuler les fichiers
        /// </summary>
        public IFileExplorer FileExplorer { get; private set; }
        #endregion

        #region CTOR
        /// <summary>
        /// Créé un nouveau formulaire de selection de destination de redimensionnement
        /// </summary>
        public OptionsDeployFileExplorer()
        {
            InitializeComponent();
            
            _profilesManager = new ProfilesManager();
            _selectedOutputs = new List<Output>();
        } 
        #endregion

        #region Private methods
        
        #region General Forms
        /// <summary>
        /// Gère le chargement du formulaire
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OptionsDeployFileExplorerLoad(object sender, EventArgs e)
        {
            LoadComboBox();

            //initialisations
            Result = OptionsResults.Cancel;
        } 
        #endregion

        #region OutputCreation
        /// <summary>
        /// Gère le bouton parcourir pour la destination
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ButtonBrowseClick(object sender, EventArgs e)
        {
            if (radioButtonLocal.Checked)
            {
                if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
                {
                    textBoxOutputPath.Text = folderBrowserDialog1.SelectedPath;
                    FileExplorer = new LocalFileExplorer();
                }
            }
            else
            {
                FTPBrowser ftpbrowser = new FTPBrowser(FTPBrowser.ModeEnum.Folder);
                if (ftpbrowser.ShowDialog() == DialogResult.OK)
                {
                    textBoxOutputPath.Text = ftpbrowser.SelectedAbsoluteFullPath;
                    FileExplorer = ftpbrowser.FileExplorer;
                }
            }
        }

        /// <summary>
        /// Gère le chargement de la combobox à partir du gestionnaire de profil
        /// </summary>
        private void LoadComboBox()
        {
            comboBoxProfiles.Items.Clear();
            comboBoxProfiles.Items.AddRange(_profilesManager.ExistingProfiles.ToArray());
            comboBoxProfiles.Items.Add(Resources.ComboBoxSeparator);
            comboBoxProfiles.Items.Add(Resources.NewProfile);
        }

        /// <summary>
        /// Gère la sélection d'un élément de la combo box de profils
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ComboBoxProfilesSelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBoxProfiles.SelectedIndex == comboBoxProfiles.Items.Count - 1)
            {
                NewProfileForm profileForm = new NewProfileForm();
                if (profileForm.ShowDialog() == DialogResult.OK)
                {
                    _profilesManager.AddProfile(profileForm.Profile);
                    LoadComboBox();
                    comboBoxProfiles.SelectedIndex = comboBoxProfiles.Items.Count - 3;
                }
            }
        }
        
        #endregion

        #region OutputList

        /// <summary>
        /// Gère l'ajout à la liste des sorties 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ButtonAddOutputClick(object sender, EventArgs e)
        {
            //Vérifications de la saisie de la destination
            if (String.IsNullOrEmpty(textBoxOutputPath.Text))
            {
                MessageBox.Show(Resources.SelectDestinationDir, Resources.Information, MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            //Vérifications de la saisie d'un profil de redimensionnement
            if (comboBoxProfiles.SelectedIndex == -1)
            {
                MessageBox.Show(Resources.SelectOutputProfile, Resources.Information, MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            //créé la sortie
            Output newOut = OutputFactory.Create((Profile)comboBoxProfiles.SelectedItem, radioButtonLocal.Checked ? OutputType.Local : OutputType.FTP, textBoxOutputPath.Text, Connection.Default);
            _selectedOutputs.Add(newOut);

            RefreshList();
        }

        private void ButtonDeleteSelectedOuptutClick(object sender, EventArgs e)
        {
            if (listViewOutputs.SelectedItems.Count > 0)
            {
                _selectedOutputs.Remove(listViewOutputs.SelectedItems[0].Tag as Output);
                RefreshList();
            }
        }

        private void ListViewOutputsSelectedIndexChanged(object sender, EventArgs e)
        {
            buttonDeleteSelectedOuptut.Enabled = (listViewOutputs.SelectedItems.Count != 0);
        }

        /// <summary>
        /// Recharge la liste des sorties
        /// </summary>
        private void RefreshList()
        {
            listViewOutputs.Items.Clear();
            foreach (Output selectedOutput in _selectedOutputs)
            {
                ListViewItem v = new ListViewItem(selectedOutput.Profile.ToString());
                v.SubItems.Add(selectedOutput.Destination.Path);
                v.Tag = selectedOutput;
                listViewOutputs.Items.Add(v);
            }
        } 
        #endregion





        #endregion



        #region Gestion des boutons (Annuler, Mode pas à pas ou mode normal)
        /// <summary>
        /// Gestion du clic sur le bouton Annuler
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ButtonCancelClick(object sender, EventArgs e)
        {
            Result = OptionsResults.Cancel;
            Close();
        }

        /// <summary>
        /// Gestion du clic sur le bouton Exécuter en mode pas à pas
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ButtonRunStepClick(object sender, EventArgs e)
        {
            Result = OptionsResults.RunStepToStep;
            Close();
        }

        /// <summary>
        /// Gestion du clic sur le bouton Exécuter en mode normal
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ButtonRunClick(object sender, EventArgs e)
        {
            Result = OptionsResults.RunNormal;
            Close();
        }
        #endregion
    }
}
